
-- inpired by Go. use defer.xxx() to make sure release resources.

local lcf = ffi.C

local o = {}

defer = o


if nil==o.hanging_cons then
	o.hanging_cons = {}
end

function o.close_connection(c)
	local co = coroutine.running()

	local t = o.hanging_cons[co]
	if nil==t then
		o.hanging_cons[co] = {c}
	else
		table.insert(t, c)
	end
end


function o.boom()
	local co = coroutine.running()

	local t = o.hanging_cons[co]
	if nil~=t then
		for i,v in ipairs(t) do
			lcf.gx_close(v)
			print('defer close ',v)
		end

		o.hanging_cons[co] = nil
	end
end
